package com.jewelry.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jewelry.entity.Product;
import com.jewelry.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("pro")
public class ProductController {
    @Autowired
    private ProductService productService;
    @GetMapping("bianji")
    public List<Product> showBianJiTuiJian(){
        List<Product> productList1 = productService.selectBiaJiTuiJia();
        return productList1;
    }
    @GetMapping("rexiao")
    public List<Product> showReXiao(){
        List<Product> productList2 = productService.selectHot();
        return productList2;
    }
    @GetMapping("jiangkong")
    public List<Product> showJiJiangShouKong(){
        List<Product> productList3 = productService.selectLessStock();
        return productList3;
    }
    @GetMapping("dijia")
    public List<Product> showDiJiaHaoHuo(){
        List<Product> productList4 = productService.selectLessPrice();
        return productList4;
    }

    @GetMapping("page")
    public Map showByNameByPage(@RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,
                                @RequestParam(value = "pageSize",defaultValue = "5")Integer pageSize,
                                @RequestParam(value = "productName",required = false,defaultValue = "钻")String productName){
        Map map = productService.selectProByNameByPage(pageNum, pageSize, productName);
        return map;
    }

    @GetMapping("shbyId")
    public Product showOneById(Integer productId){
        Product product = productService.getById(productId);
        return product;
    }

    @PostMapping
    public Map addProduct(@RequestBody Product product){
        Map map = new HashMap();
        try {
            product.setSale(0);
            productService.save(product);
            map.put("message","success");
        }catch (Exception e){
            e.printStackTrace();
            map.put("message","error");
        }
        return map;
    }

    @DeleteMapping("{productId}")
    public Map deleteProduct(@PathVariable("productId") Integer productId) {
        Map<String, String> map = new HashMap<>();
        try {
            QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("product_id", productId);
            productService.remove(queryWrapper);
            map.put("message", "success");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("message", "error");
        }
        return map;
    }
    @PutMapping
    public Map updateProduct(@RequestBody Product product) {
        Map<String, String> map = new HashMap<>();
        try {//updateById(product)
            boolean success = productService.updateById(product); // 根据产品ID更新产品信息
            if (success) {
                map.put("message", "success");
            } else {
                map.put("message", "error: product not found");
            }
        } catch (Exception e) {
            e.printStackTrace();
            map.put("message", "error");
        }
        return map;
    }

    @GetMapping
    public Map<String, Object> getProduct(Integer pId,
                                          Integer cId,
                                          @RequestParam(defaultValue = "1") Integer pageNum,
                                          @RequestParam(defaultValue = "5") Integer pageSize) {
        HashMap<String, Object> map = new HashMap<>();
        Page<Product> products = productService.getProductByCategory(pId, cId, pageNum, pageSize);
        map.put("code", 200);
        map.put("message", "success");
        map.put("data", products);
        return map;
    }

    @GetMapping("showAll")
    public List<Product> showAllPPPPP(){
        List<Product> list = productService.list();
        return list;
    }


    @PostMapping("/upload")
    public String upload(MultipartFile file) throws IOException {

        if (file.isEmpty()) {
            return "null";
        }

        String fileName = file.getOriginalFilename();
        InputStream is = file.getInputStream();

        //获取项目根目录的绝对路径
        String rootPath = System.getProperty("user.dir");

        //构建文件保存目录的路径，可以使用以下代码：

        String filePath = rootPath + "/src/main/resources/static/front/images/jewelry/";


        // 创建输出流
        OutputStream os = new FileOutputStream(filePath + fileName);

        // 将文件内容从输入流复制到输出流
        FileCopyUtils.copy(is, os);

        os.close();
        is.close();

        // 返回上传文件在服务器上的相对路径
        return fileName;
    }
}

